package com.strong.faker;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.strong.enum_.MobilePhoneEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 手机 模拟数据生成类
 *
 * @author Administrator
 * @date 2023/04/08
 */
@Slf4j
public class MobilePhoneFaker extends BaseFaker<String> {
    /**
     * 手机号段枚举
     */
    private MobilePhoneEnum[] mobilePhoneEnums;

    /**
     * 用于生成号码的号码段数组
     */
    private String[] strsPrefix;

    /**
     * 是否初始化
     */
    private boolean isInitialized = false;

    /**
     * 注入号码段前缀
     *
     * @param mobilePhoneEnums 手机枚举数组
     * @return {@link MobilePhoneFaker}
     */
    public MobilePhoneFaker prefix(MobilePhoneEnum... mobilePhoneEnums) {
        this.mobilePhoneEnums = mobilePhoneEnums;
        this.isInitialized = false;
        return this;
    }

    @Override
    protected void initialize() {
        // 如果枚举数组为空，则默认所有枚举
        if (ArrayUtil.isEmpty(this.mobilePhoneEnums)) {
            this.mobilePhoneEnums = MobilePhoneEnum.values();
        }

        // 如果前缀数组为空，则初始化数组
        if (!this.isInitialized || ArrayUtil.isEmpty(this.strsPrefix)) {
            this.strsPrefix = ArrayUtil.addAll(Arrays.stream(this.mobilePhoneEnums).map(MobilePhoneEnum::getPrefix).toArray(String[][]::new));
            this.isInitialized = true;
        }

        log.info("【MobilePhoneFaker】手机号码段参数：{}", ArrayUtil.toString(this.strsPrefix));
    }

    @Override
    protected String getModel() {
        StringBuilder sbMobilePhone = new StringBuilder(RandomUtil.randomEle(this.strsPrefix));
        long longMobilePhone = RandomUtil.randomLong(99999999L);
        sbMobilePhone.append(StrUtil.padPre(Long.toString(longMobilePhone), 8, "0"));
        return sbMobilePhone.toString();
    }
}
